<template>
	<view class="page">
		<u-navbar :isBack="true" title="用户注册" title-color="#101010" 
		:background="{background: '#fff'}" :borderBottom="false"></u-navbar>
		
		
		<view style="padding: 32rpx 24rpx;">
			<view style="padding: 40rpx 24rpx;background: #fff;border-radius: 24rpx;">
				<view style="display: flex;align-items: center;font-size: 28rpx;
				border-bottom: #E7E7E7 solid 1rpx;padding-bottom: 40rpx;">
					<view style="color: #333333;width: 140rpx;">手机号</view>
					<view style="flex: 1;margin-left: 40rpx;">
						<input type="text" placeholder="请填写手机号" v-model="formData.mobile" maxlength="11">
					</view>
				</view>
				<view style="border-bottom: #EFEFEF 2rpx solid;padding: 32rpx 0;display: flex;align-items: center;">
					<view style="display: flex;align-items: center;">
						<view style="color: #333333;width: 140rpx;">验证码</view>
						<view style="flex: 1;margin-left: 40rpx;">
							<input type="number" placeholder="请填写验证码" v-model="formData.recode" maxlength="11">
						</view>
					</view>
					<view style="color: #257CF6;font-size: 28rpx;" @click="sendPhoneCode">
						{{ codeBtn.text }}
					</view>
				</view>
				<view style="display: flex;align-items: center;font-size: 28rpx;
				border-bottom: #E7E7E7 solid 1rpx;padding: 40rpx 0;">
					<view style="color: #333333;width: 140rpx;">登录密码</view>
					<view style="flex: 1;margin-left: 40rpx;">
						<input type="text" placeholder="请填写登录密码" v-model="formData.password">
					</view>
				</view>
				<view style="display: flex;align-items: center;font-size: 28rpx;
				border-bottom: #E7E7E7 solid 1rpx;padding: 40rpx 0;">
					<view style="color: #333333;width: 140rpx;">姓名</view>
					<view style="flex: 1;margin-left: 40rpx;">
						<input type="text" placeholder="请填写真实姓名" v-model="formData.nickname">
					</view>
				</view>
				<view style="display: flex;align-items: center;margin-top: 40rpx;font-size: 28rpx;">
					<view style="color: #333333;width: 140rpx;">身份证号</view>
					<view style="flex: 1;margin-left: 40rpx;">
						<input type="text" placeholder="请填写您的身份证号" v-model="formData.idcode">
					</view>
				</view>
			</view>
			
			<view style="padding: 32rpx 24rpx;background: #fff;border-radius: 24rpx;margin-top: 24rpx;">
				<view style="display: flex;align-items: center;">
					<view style="flex: 1;font-size: 28rpx;justify-content: space-between;display: flex;">
						<view style="color: #333333;">身份证</view>
					</view>
				</view>
				<view style="background: #f8fbff;padding: 32rpx 56rpx;margin-top: 24rpx;" 
				@click="changeIdImage('idcode_zheng')">
					<image v-if="formData.idcode_zheng" :src="formData.idcode_zheng" mode="widthFix" style="width: 100%;"></image>
					<image v-else src="@/static/my/sfz.png" mode="widthFix" style="width: 100%;"></image>
				</view>
				
				<view style="background: #f8fbff;padding: 32rpx 56rpx;margin-top: 24rpx;" 
				@click="changeIdImage('idcode_fang')">
					<image v-if="formData.idcode_fang" :src="formData.idcode_fang" mode="widthFix" style="width: 100%;"></image>
					<image v-else src="@/static/my/sff.png" mode="widthFix" style="width: 100%;"></image>
				</view>
			</view>
			
			<view style="padding: 32rpx 24rpx;background: #fff;border-radius: 24rpx;margin-top: 24rpx;">
				<view style="display: flex;align-items: center;font-size: 28rpx;
				border-bottom: #E7E7E7 solid 1rpx;padding-bottom: 40rpx;">
					<view style="color: #333333;width: 140rpx;">视频号</view>
					<view style="flex: 1;margin-left: 40rpx;">
						<input type="text" placeholder="请填写视频号ID" v-model="formData.shipinhao">
					</view>
				</view>
				<view style="margin-top: 24rpx;color: #333A47;font-size: 28rpx;">
					视频小店二维码
				</view>
				<view style="margin-top: 16rpx;display: flex;flex-wrap: wrap;">
					<view class="img_add" style="display: inline-block;" @click="changeIdImage('shop_qrcode')">
						<image v-if="formData.shop_qrcode" :src="formData.shop_qrcode" mode="aspectFill" 
						style="width: 180rpx; height: 180rpx;"></image>
						<image v-else src="@/static/login/img_add.png" mode="aspectFill" 
						style="width: 180rpx; height: 180rpx;"></image>
					</view>
				</view>
				
			</view>
			
			<view style="color: #999999;font-size: 24rpx;margin-top: 24rpx;">请如实填写上述相关信息</view>
			
			<view class="login-title-info" @click="agreementBtn" style="margin-top: 24rpx;">
				<checkbox-group>
					<label>
						<checkbox value="cb" color="#50A5F4" style="transform:scale(0.7)" />
						我已阅读并同意
						<text @click="jumpTo('/pages/my/protocol?type=user')" style="color:#3D7EFF">《用户协议与隐私协议》</text>
					</label>
				</checkbox-group>
			</view>
			<view style="margin-top: 32rpx;padding: 24rpx;color: #fff;
			border-radius: 16rpx;text-align: center;background: #50A5F4;" @click="onSubmin">
				提交注册
			</view>
		</view>
		
		<!-- <view style="position: fixed;bottom: 60rpx;width: 100%;">
			<image src="../../static/bottomimg.png" mode="widthFix" style="width: 100%;"></image>
		</view> -->
		<u-toast ref="uToast" />
		
	</view>
</template>

<script>
	import appurl_config from 'utils/config.js'
	import {request} from 'utils/request.js'
	import Vue from 'vue'
	var moment = require('moment');
	export default {
		data() {
			return {
				agreementFlag: false, //注册协议选中状态
				
				seconds: 60,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				},
				disableCodeBtn: false,
				
				formData: {
					mobile: '',
					password: '',
					nickname: '',
					idcode: '',
					
					idcode_zheng: '',
					idcode_fang: '',
					shipinhao: '',
					shop_qrcode: '',
					
					recode: '',
					event: 'register'
				}
			}
		},
		onLoad() {
			// 禁止分享
			uni.hideShareMenu();
			// 获取配置信息
			// request.get({
			// 	url: '/basicinfo/agreement',
			// 	data: {'type': 'user'}
			// }).then(res=>{
			// 	console.log('返回用户信息：', res);
			// 	// return;
			// 	if(res.code == 1){
			// 		this.$refs.uToast.show({ title: '注册成功' });
			// 		// uni.setStorageSync('userToken', userinfo.token);
			// 		setTimeout(() => {
			// 			uni.navigateBack();
			// 			// uni.$emit('htmlRefresh', {
			// 			// 	type: 1
			// 			// });
			// 		}, 300);
			// 	}else{
			// 		this.$refs.uToast.show({ title: res.msg });
			// 	}
			// })
		},
		onShow(){},
		methods: {
			sendPhoneCode: function() {
				if(this.disableCodeBtn){
					return;
				}
				// if (this.rephone == '' || !this.isphone(this.rephone)) {
				if (this.formData.mobile == '') {
					uni.showToast({
						title: '手机号码不能为空',
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (this.codeBtn.waitingCode == false) {
					// let data = {};
					// data.phone = this.rephone;
					request.get({
						url: '/smsmessage/generateCaptcha',
						data: {
							phone: this.formData.mobile,
							event: 'register',
						}
					}).then(res=>{
						console.log(res);
						if(res.code == 1){
							this.disableCodeBtn = true;
							this.codeBtn.waitingCode = true;
							this.codeBtn.count = this.seconds;
							this.codeBtn.text = this.codeBtn.count + 's';
							let countdown = setInterval(() => {
								this.codeBtn.count--;
								this.codeBtn.text = this.codeBtn.count + 's';
								if (this.codeBtn.count < 0) {
									clearInterval(countdown);
									this.codeBtn.text = '重新发送';
									this.codeBtn.waitingCode = false;
									this.disableCodeBtn = false;
								}
							}, 1000);
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
							return;
						}
					})
				}else{
					return;
				}
				
			},
			// 上传身份证
			changeIdImage(etype) {
			    let that = this;
				let userToken = uni.getStorageSync('userToken');
			    uni.chooseImage({
			        count: 1,
			        sizeType: ['compressed'],
			        sourceType: ['album','camera'],
			        success: (chooseImageRes) => {
			            uni.uploadFile({
			                url: appurl_config.appurl + '/common/upload', //请求地址
			                filePath: chooseImageRes.tempFilePaths[0],
			                name: "file",
			                header: {
			                    // 设置请求的 header
			                    token: userToken,
			                },
			                success: (res) => {
								console.log('JSON.parse(res.data):', JSON.parse(res.data))
								that.formData[etype] = JSON.parse(res.data).data.fullurl;
								// that.profileUserInfo('avatar');
			                },
			                fail: (err) => {
			                    uni.showToast({
			                        title: err.msg,
			                        icon: 'none'
			                    })
			                    console.log('uploadFile_err: ', err);
			                },
			            });
			        }
			    });
			},
			jumpTo(path){
				uni.navigateTo({
					url: path
				})
			},
			// 授权登录
			authorLogin(){
				let _this = this;
				uni.login({
					success(res) {
						if(res.code){
							_this.onSubmin(res.code);
						}
					}
				})
			},
			//选中注册协议按钮
			agreementBtn() {
				this.agreementFlag = !this.agreementFlag;
			},
			onSubmin(){
				if (!this.agreementFlag) {
					uni.showToast({
						title: "请先阅读并勾选用户协议",
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				
				let params = {};
				params = this.formData;
				params.pid = uni.getStorageSync('pid');
				// if(this.formData.mobile == ''){
				// 	this.$refs.uToast.show({ title: '手机号码不能为空' })
				// 	return;
				// }
				// if(this.formData.shipinhao == ''){
				// 	this.$refs.uToast.show({ title: '视频号不能为空' })
				// 	return;
				// }
				request.postWithToken({
					url: '/user/register',
					data: params
				}).then(res=>{
					console.log('返回用户信息：', res);
					// return;
					if(res.code == 1){
						this.$refs.uToast.show({ title: '注册成功' });
						// uni.setStorageSync('userToken', userinfo.token);
						setTimeout(() => {
							uni.navigateBack();
							// uni.$emit('htmlRefresh', {
							// 	type: 1
							// });
						}, 300);
					}else{
						this.$refs.uToast.show({ title: res.msg });
					}
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #F7F7F7 !important;
	}
</style>
<style lang="scss" scoped>
	.top-box{
		background-size: 100% 100%;
	}
</style>
